package com.kevin.jdbc.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

import com.kevin.jdbc.daoUtil.CommonDataSource;

public abstract class BaseDao {

	public Object select(String sql, Object[] args) {

		Connection conn = null;
		PreparedStatement ps = null;
		ResultSet rs = null;

		List<Object> list = new ArrayList<Object>();

		try {

			conn = CommonDataSource.getConnection();
			ps = conn.prepareStatement(sql);

			for (int i = 0; i < args.length; i++) {
				ps.setObject(i + 1, args[i]);
			}

			rs = ps.executeQuery();

			while (rs.next()) {
				list.add(rowMapper(rs));
			}

		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			this.free(rs, ps, conn);
		}

		return list;
	}

	public Object update(String sql, Object[] args) throws SQLException {

		Connection conn = null;
		PreparedStatement ps = null;
		ResultSet rs = null;

		try {

			conn = CommonDataSource.getConnection();
			ps = conn.prepareStatement(sql);

			for (int i = 0; i < args.length; i++) {
				ps.setObject(i + 1, args[i]);
			}

			rs = ps.executeQuery();

		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			this.free(rs, ps, conn);
		}

		return this.rowMapper(rs);
	}

	public Object delete(String sql, Object[] args) throws SQLException {

		Connection conn = null;
		PreparedStatement ps = null;
		ResultSet rs = null;

		try {

			conn = CommonDataSource.getConnection();
			ps = conn.prepareStatement(sql);

			for (int i = 0; i < args.length; i++) {
				ps.setObject(i + 1, args[i]);
			}

			rs = ps.executeQuery();

		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			this.free(rs, ps, conn);
		}

		return this.rowMapper(rs);
	}

	public Object insert(String sql, Object[] args) throws SQLException {

		Connection conn = null;
		PreparedStatement ps = null;
		ResultSet rs = null;

		try {

			conn = CommonDataSource.getConnection();
			ps = conn.prepareStatement(sql);

			for (int i = 0; i < args.length; i++) {
				ps.setObject(i + 1, args[i]);
			}

			rs = ps.executeQuery();

		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			this.free(rs, ps, conn);
		}

		return this.rowMapper(rs);
	}

	public abstract Object rowMapper(ResultSet rs) throws SQLException;

	public void free(ResultSet rs, Statement st, Connection conn) {
		try {
			if (rs != null) {
				rs.close();
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {

			try {
				if (st != null) {
					st.close();
				}
			} catch (SQLException e) {
				e.printStackTrace();
			} finally {
				if (conn != null) {
					try {
						conn.close();
					} catch (SQLException e) {
						e.printStackTrace();
					}
				}
			}
		}
	}
}
